package com.colladaviewer.app;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import com.colladaviewer.android.camera.AndroidARManager;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.input.android.AndroidInput;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix4f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.scene.shape.Quad;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import com.jme3.system.android.OGLESContext;
import com.neotank.app.AndroidApplication;
import com.neotank.app.NeoTank;
import com.neotank.input.VirtualJoystickElement;
import com.neotank.input.VirtualJoystickElement.ElementType;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class MainActivity extends Activity 
{

        
    /**
    *
    * @author as
    */

       private final static Logger logger = Logger.getLogger(MainActivity.class.getName());
       
       private OGLESContext ctx;
       private GLSurfaceView view;
       private Application app = null;

       @Override
       public void onCreate(Bundle savedInstanceState) {
           
           super.onCreate(savedInstanceState);
           
           Logger log = logger;
           boolean bIsLogFormatSet = false;
           do
           {
               if (log.getHandlers().length == 0)
               {
                   log = logger.getParent();
                   if (log != null)
                       for (Handler h : log.getHandlers())
                       {
                           h.setFormatter(new SimpleFormatter());
                           bIsLogFormatSet = true;
                       }
               }
           } while (log != null && !bIsLogFormatSet);
           
           
           JmeSystem.setResources(getResources());

           requestWindowFeature(Window.FEATURE_NO_TITLE);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
           WindowManager.LayoutParams.FLAG_FULLSCREEN);
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

           AppSettings settings = new AppSettings(true);                               
           AndroidInput input = new AndroidInput(this);
           
           AndroidApplication app = new ARScene(this, input);

           
           logger.info("setting settings ...");
           app.setSettings(settings);
           logger.info("setting settings ... done.");

           logger.info("starting app ...");
           app.start();
           logger.info("starting app ... done.");

           logger.info("creating context ...");
           ctx = (OGLESContext) app.getContext();
           logger.info("creating context ... done.");

           logger.info("creating view ...");
           view = ctx.createView(input);
           logger.info("creating view ... done.");

           logger.info("setting content view ...");
           setContentView(view);
           logger.info("setting content done ...");
       }

       @Override 
       protected void onResume() {
           logger.info("onResume ...");
           super.onResume();
           logger.info("view.onResume ...");

           view.onResume();

           logger.info("view.onResume ... done.");
           logger.info("onResume ... done.");
       }

       @Override
       protected void onPause() {
           super.onPause();
           app.stop();
           view.onPause();           
       }
       


}
